#include "gobangtimer.h"

#include <QLayout>
#include <QMessageBox>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gobangboard.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    init_widgets();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init_widgets() {
    window = new QWidget;
    setCentralWidget(window);
    QVBoxLayout *layout = new QVBoxLayout(window);
    QHBoxLayout *top_layout = new QHBoxLayout;
    QHBoxLayout *bottom_layout = new QHBoxLayout;
    layout->addLayout(top_layout);
    layout->addLayout(bottom_layout);
    GobangBoard* gobang_board = new GobangBoard;
    GobangTimer* gobang_timer = new GobangTimer;
    bottom_layout->addWidget(gobang_board);
    bottom_layout->addWidget(gobang_timer);

    connect(gobang_board, SIGNAL(game_over(int, std::string)),
            this, SLOT(game_over(int, std::string)));
    connect(ui->actionNew_Game, SIGNAL(triggered()), this, SLOT(game_start()));
    setFixedSize(ui->menubar->minimumSizeHint() + window->minimumSizeHint());
    setWindowTitle(QString("GoBang"));
}

void MainWindow::game_start() {
    QWidget* old_window = window;
    init_widgets();
    delete old_window;
}

void MainWindow::game_over(int winner, std::string message) {
    std::string winner_str = (winner==0)?"Black":"White";
    std::string win_message = "Winner is " +winner_str + ". Win by " + message;
    QMessageBox::information(this, "Game Over", QString::fromStdString(win_message));
}
